<?php
namespace PsiFramework\Classes\Util\UI;

import("Classes.DAO.LanguageDAO");
import("Classes.Util.HTTP.Session");
import("Classes.Util.Manager.ParametersManager");

use \PsiFramework\Classes\DAO\LanguageDAO;
use \PsiFramework\Classes\Util\HTTP\Session;
use \PsiFramework\Classes\Util\Manager\ParametersManager;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The LanguageManager class is used to manage the languages
 *
 * @package Util.UI
 */
class LanguageManager
{

    protected static $_singleton = null;

    protected /*string*/                $_lang ;
    protected /*Session*/               $_session ;
    protected /*array<string, string>*/ $_texts ;
    protected /*object*/                $_localisedConfig ;

    /**
     * Constructor
     *
     * @return    LanguageManager    instance
     */
    protected function __construct()
    {
        $this->_lang = null ;
        $this->_session = Session::getInstance();

        $lang = null ;
        $sessionLang = $this->_session->get("lang");
        if (!isNullOrEmptyString($sessionLang)) {
            $lang = $sessionLang ;
        } else {
            $lang
                = ParametersManager::getInstance()
                   ->getApplicationsParameters()
                   ->getDefaultLanguage();
        }

        if ($lang != $sessionLang) {
            $this->setLanguage($lang);
        } else {
            $this->_lang = $sessionLang ;
            $this->_texts = $this->_session->get("texts");
            $this->_localisedConfig = $this->_session->get("localisedConfig");
        }
    }

    public function setLanguage($lang)
    {
        if ($this->_lang != $lang) {
            $this->_lang = $lang ;
            $this->_session->set("lang", $lang);

            $this->_texts = LanguageDAO::getInstance()->getTexts($lang);
            $this->_session->set("texts", $this->_texts);

            $this->_localisedConfig
                = json_decode(
                    file_get_contents(
                        getConfigFile('i18n/'.$this->_lang.'.json')
                    )
                );
            $this->_session->set("localisedConfig", $this->_localisedConfig);
        }
    }

    public function getLanguage()
    {
        return $this->_lang ;
    }

    /**
     * Returns the localized text
     *
     * @param     string     $key    the key of the localized text
     * @param    string    value
     */
    public function getText(/*string*/ $key)
    {
        $return = null ;
        if (isset($this->_texts[$key])) {
            $return = $this->_texts[$key] ;
        }
        if (isNullOrEmptyString($return)) {
            $return = $key ;
        }
        return $return ;
    }

    public function getLocalisedConfig()
    {
        return $this->_localisedConfig ;
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @param     string             $lang        the language to retrieve
     * @return     LanguageManager instance
     */
    public static function getInstance()
    {

        if (self::$_singleton==null) {
            self::$_singleton = new LanguageManager();
        }

        return self::$_singleton;
    }
}
